﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using System.Collections;

namespace HQWeb.SysManage.EntyTypeManage
{
    public partial class EntyTypeEdit : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                VerdictIsLogin();
                if (!Page.IsPostBack)
                {
                    BindElementList();
                    Sign.Value = Request["sign"];

                    if (Sign.Value == "0")  //添加
                    {
                        PIdHid.Value = Request["pid"];
                        PID.SelectedValue = PIdHid.Value;
                    }
                    BindTypeList();
                    if (Sign.Value == "1")  //编辑
                    {
                        PIdHid.Value = Request["id"];
                        PID.SelectedValue = PIdHid.Value;
                        fillForm();
                    }
                }
            }
            catch (Exception ex)
            {
                Universal.JS.Alert(ex.Message);
            }
        }

        #region 绑定元素分类下拉框
        private void BindElementList()
        {

            BLL.SysManage.T_PUB_MATERIALTYPE MaterialTypeBll = new BLL.SysManage.T_PUB_MATERIALTYPE();
            IList il = MaterialTypeBll.GetModelList(" ISUSED=1");
            PID.ClearSelection();
            PID.DataSource = il;
            PID.DataTextField = "TYPE_NAME"; //dropdownlist的Text的字段 
            PID.DataValueField = "TYPE_ID";//dropdownlist的Value的字段 
            PID.DataBind();
            ListItem item = new ListItem("无上级", "0");
            PID.Items.Add(item);
        }
        #endregion

        #region 绑定所属分类下拉框
        private void BindTypeList()
        {
            ListItem item1 = new ListItem("无需采购并无需出入库", "0");
            ListItem item2 = new ListItem("需采购", "1");
            ListItem item3 = new ListItem("需采购并需出入库", "2");
            ddl_type.Items.Add(item1);
            ddl_type.Items.Add(item2);
            ddl_type.Items.Add(item3);

        }
        #endregion

        #region 填充页面数据
        protected void fillForm()
        {
            BLL.SysManage.T_PUB_MATERIALTYPE MaterialTypeBll = new BLL.SysManage.T_PUB_MATERIALTYPE();
            Model.SysManage.T_PUB_MATERIALTYPE MaterialTypeModel = new Model.SysManage.T_PUB_MATERIALTYPE();
            int temp;
            int.TryParse(IdHid.Value, out temp);
            MaterialTypeModel = MaterialTypeBll.GetModel(temp);
            this.ddl_type.SelectedValue = MaterialTypeModel.FALG.ToString();
            this.txtName.Text = MaterialTypeModel.TYPE_NAME;
            this.txtremark.Text = MaterialTypeModel.REMARK;
            this.PID.SelectedValue = MaterialTypeModel.PARENT_ID.ToString();
            PIdHid.Value = MaterialTypeModel.PARENT_ID.ToString();
        }
        #endregion

        #region 提交按钮事件
        protected void btnSend_Click(object sender, EventArgs e)
        {
            try
            {
                BLL.SysManage.T_PUB_MATERIALTYPE MateriaTypelBll = new BLL.SysManage.T_PUB_MATERIALTYPE();
                Model.SysManage.T_PUB_MATERIALTYPE MateriaTypeModel = new Model.SysManage.T_PUB_MATERIALTYPE();
                MateriaTypeModel.TYPE_ID = Convert.ToInt32(string.IsNullOrEmpty(IdHid.Value) ? "-1" : IdHid.Value);
                MateriaTypeModel.PARENT_ID = Convert.ToInt32(this.PID.SelectedValue.Trim());
                MateriaTypeModel.TYPE_NAME = this.txtName.Text.Trim();
                MateriaTypeModel.REMARK = this.txtremark.Text.Trim();
                MateriaTypeModel.FALG = Convert.ToInt32(this.ddl_type.SelectedValue.ToString());
                MateriaTypeModel.ISUSED = 1;
                if (Sign.Value == "0")
                {

                    MateriaTypelBll.Add(MateriaTypeModel);
                    Universal.CommonMethod.SaveOpreation(Convert.ToInt32(Session["USER_ID"]), "添加" + MateriaTypeModel.TYPE_NAME + "元素分类", DateTime.Now);
                }
                else if (Sign.Value == "1")
                {

                    MateriaTypelBll.Update(MateriaTypeModel);
                    Universal.CommonMethod.SaveOpreation(Convert.ToInt32(Session["USER_ID"]), "更新" + MateriaTypeModel.TYPE_NAME + "元素分类信息", DateTime.Now);
                }
                string strUrl = string.Format("MaterialList.aspx?pid={0}", MateriaTypeModel.PARENT_ID);
                Response.Redirect(strUrl);
                //StringBuilder strl = new StringBuilder("<script language='javascript'>\n");
                //strl.Append("window.parent.location.reload();\n");
                //strl.Append("location.href =\"ElementTypeList.aspx?pid=" + this.PID.SelectedValue + "\";\n");
                //strl.Append("</script>\n");
                //HttpContext.Current.Response.Write(strl.ToString());
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
                //COM.HS.Utility.JScript.Alert(ex.Message);
                //return;
            }
            //catch
            //{
            //    throw;
            //}
        }
        #endregion

    }
}